/**
 * 任务列表js
 * date: 2011-2-1
 */
COMM.lib.load("dialog");
(function($){

	var tableBody;
	
	var Operation = {
		start : function(taskId){
			$.getJSON("/admin/task/start.do?taskId="+taskId, function(data){
				if(data.result == 1){
					alert("启动成功");
					TaskList.getData();
				} else {
					alert("启动失败");
				}
			});	
		},
		stop : function(taskId){
			$.getJSON("/admin/task/stop.do?taskId="+taskId, function(data){
				if(data.result == 1){
					alert("停止成功");
					TaskList.getData();
				} else {
					alert("停止失败");
				}
			});	
		},
		execute : function(taskId){
			$.getJSON("/admin/task/executeTask.do?taskId="+taskId, function(data){
				if(data.result == 1){
					alert("执行成功");
					TaskList.getData();
				} else {
					alert("执行失败");
				}
			});	
		}
	};
	
	var TaskList = {
		getData : function(){
			$.postJson({
				url: "/admin/task/list.do",
				success: function(data){
					TaskList.render(data.taskList);
				}
			});
		},
		render : function(taskList){
			var tbody = $("<tbody></tbody>");
			var task,tr,td, lastExcuteDate,nextExcuteDate;
			for(var i=0;i<taskList.length;i++){
				task = taskList[i];
				tr = $("<tr class='" + ((i+1)%2==0 ? "even" : "odd") + "'></tr>");
				tr.append("<td class='tc'>"+task.id+"</td>");
				tr.append("<td>"+task.name+"</td>");
				if(task.lastExcuteDate){
					lastExcuteDate = task.lastExcuteDate;
				} else {
					lastExcuteDate = "未运行";
				}
				tr.append("<td class='tc'>"+lastExcuteDate+"</td>");
				tr.append("<td class='tc'>"+task.lastExcuteStateDesc+"</td>");
				if(task.nextExcuteDate){
					nextExcuteDate = task.nextExcuteDate;
				} else {
					nextExcuteDate = "未知";
				}
				tr.append("<td class='tc'>"+nextExcuteDate+"</td>");
				tr.append("<td class='tc'>"+task.runningDesc+"</td>");
				td = $("<td class='tc'></td>");
				td.append( $("<a class='icon icon-start'></a>").click(function(id){
					return function(){
						Operation.start(id);
					};
				}(task.id)));
				td.append($("<a class='icon icon-pause'></a>").click(function(id){
					return function(){
						Operation.stop(id);
					};
				}(task.id)));
				td.append($("<a class='icon icon-refresh'></a>").click(function(id){
					return function(){
						Operation.execute(id);
					};
				}(task.id)));
				tbody.append(tr.append(td));
			}
			tableBody.empty().append(tbody.children());
			tbody = null;
		}
	};
	
	var TaskAll = {
		init : function(){
			$("#task_rebuildIndex").click(function(){
				$.getJSON("/admin/task/rebuildIndex.do", function(data){
					if(data.result == 1){
						alert("重建成功");
					} else {
						alert("重建失败");
					}
				});	
			});
			$("#task_startAll").click(function(){
				TaskAll.startAll();
			});
			$("#task_stopAll").click(function(){
				TaskAll.stopAll();
			});
			$("#task_reload").click(function(){
				TaskAll.reload();
			});
		},
		startAll : function(){
			$.postJson({
				url: "/admin/task/startAll.do",
				success: function(data){
					if(data.result == 1){
						alert("启动成功");
						TaskList.getData();
					} else {
						alert("启动失败");
					}
				}
			});
		},
		stopAll : function(){
			$.postJson({
				url: "/admin/task/stopAll.do",
				success: function(data){
					if(data.result == 1){
						alert("停止成功");
						TaskList.getData();
					} else {
						alert("停止失败");
					}
				}
			});
		},
		reload : function(){
			$.postJson({
				url: "/admin/task/reload.do",
				success: function(data){
					if(data.result == 1){
						alert("加载成功");
						TaskList.getData();
					} else {
						alert("加载失败");
					}
				}
			});
		}
	}
	$(document).ready(function(){
		tableBody = $("#content #tl-tbody");
		TaskList.getData();
		TaskAll.init();
	});
})(jQuery);